package com.sheng.sign.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.sheng.sign.entity.Course;

public class CourseDaoImpl implements CourseDao {
	
	 private SessionFactory sessionFactory;
	 
	 
		public SessionFactory getSessionFactory() {
			return sessionFactory;
		}
		public void setSessionFactory(SessionFactory sessionFactory) {
			this.sessionFactory = sessionFactory;
		}

	@Override
	public boolean add(Course course) {
		try {
			course.setIsDelete(0);
			sessionFactory.getCurrentSession().save(course);
			System.out.println("==来到了DaoImpl=="+course);
			return true;
		} catch (HibernateException e) {
			return false;
		}

	}

	@Override
	public boolean edit(Course course) {
		try {
			course.setIsDelete(0);
			sessionFactory.getCurrentSession().update(course);
			System.out.println("==来到了DaoImpl=="+course);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}

	@Override
	public boolean delete(Course course) {
		try {
			course.setIsDelete(1);
			sessionFactory.getCurrentSession().update(course);
			System.out.println("==来到了DaoImpl=="+course);
			return true;
		} catch (HibernateException e) {
			return false;
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Course> findAllList() {
		System.out.println("===来到了课程dao的findAll===");
		String hql = "from Course where 1=1 and is_delete=0";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}
	public Course findById(Course course) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Course where id='"+course.getId()+"'");  
		List list = query.list();  
		if (null != list && 0 < list.size()) {
			course = (Course) list.get(0);
		}
		return course;
	}
	@Override
	public List<Course> findByCourseName(Course course) {
		String hql = "from Course where 1=1 and course_name like '%"+course.getCourseName()+"%' and is_delete=0";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);  
        List list = query.list();  
        return list;
	}
	
	//单个查询  
//    public User getUser(Integer id) throws Exception {  
//        return (User) sessionFactory.getCurrentSession().createQuery("from User u where u.id ="+id).uniqueResult();  
//    }  
	

}
